package RSSReader;


import java.io.*;
import java.util.Vector;

import org.kxml.parser.*;
import org.kxml.Xml;


/**
 *
 * @author Christian
 */

public class rssConnector
{
    
    
  // RSS DIREKT PARSEN
  public Vector parse(InputStream in)
  {

    // VARS
    boolean trucking = true;
    boolean first = true;
    Reader reader = new InputStreamReader(in);
    XmlParser parser = null;

    // VEKTOR FÜR EMPFANGENE RSS FEEDS
    Vector rssItems = new Vector();


    try
    {
        parser = new XmlParser(reader);
        ParseEvent pe = null;

        parser.skip();
        parser.read(Xml.START_TAG, null, "rss");
        parser.skip();
        parser.read(Xml.START_TAG, null, "channel");


        // RSS DURCHSURCHEN
        while (trucking)
        {
          pe = parser.read();
          if (pe.getType() == Xml.START_TAG)
          {
            String name = pe.getName();
            if (name.equals("item"))
            {
              String title, link, description;
              title = link = description = null;
              while ((pe.getType() != Xml.END_TAG) || (pe.getName().equals(name) == false))
              {
                pe = parser.read();
                if (pe.getType() == Xml.START_TAG && pe.getName().equals("title"))
                {
                  pe = parser.read();
                  title = pe.getText();
                }
                else if (pe.getType() == Xml.START_TAG && pe.getName().equals("link"))
                {
                  pe = parser.read();
                  link = pe.getText();
                }
                else if (pe.getType() == Xml.START_TAG && pe.getName().equals("description"))
                {
                  pe = parser.read();
                  description = pe.getText();
                }
              }

              // RSS ITEM ELEMENT ANLEGEN UND IN VEKTOR ADDEN
              rssItem item = new rssItem(title, link, description);
              rssItems.addElement(item);

            }
            else
            {
              while ((pe.getType() != Xml.END_TAG) || (pe.getName().equals(name) == false))
                pe = parser.read();
            }
          }
          if (pe.getType() == Xml.END_TAG &&  pe.getName().equals("rss"))
            trucking = false;
        }
    }
    catch (IOException ignored) { }

    return rssItems;
  }
}
